package de.dis2011.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class stadt extends region
{
    private int stadtid = -1;
    private String stadtname;
    
    public int getStadtid()
    {
        return stadtid;
    }

    public void setStadtid(int stadtid)
    {
        this.stadtid = stadtid;
    }

    public String getStadtname()
    {
        return stadtname;
    }

    public void setStadtname(String stadtname)
    {
        this.stadtname = stadtname;
    }

    /**
     * Lädt eine Information aus der Datenbank
     * 
     * @param id
     * 
     * @return Informations-Instanz
     */
    public static stadt load(int id)
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM stadtid WHERE stadtid = ?";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);
            pstmt.setInt(1, id);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
            {
                stadt ts = new stadt();
                ts.setStadtid(rs.getInt("stadtid"));
                ts.setRegionid(rs.getInt("regionid"));
                ts.setStadtname(rs.getString("name"));
                System.out.println("ID: " + ts.getStadtid() + "ID: " + ts.getRegionid() + " Name: " + ts.getStadtname());
            }
            rs.close();
            pstmt.close();

        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static stadt loadAll()
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM stadtid";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                stadt ts = new stadt();
                ts.setStadtid(rs.getInt("stadtid"));
                ts.setRegionid(rs.getInt("regionid"));
                ts.setStadtname(rs.getString("name"));
                System.out.println("ID: " + ts.getStadtid() + "ID: " + ts.getRegionid() + " Name: " + ts.getStadtname());
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}
